package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.PicShareMapper;
import com.qf.dao.PicShareRespository;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.PicShare;
import com.qf.service.PicShareService;
import com.qf.utils.RedisUtils;
import org.apache.tomcat.jni.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Service
public class PicShareServiceImpl implements PicShareService {
    @Autowired
    PicShareRespository picShareRespository;
    @Autowired
    PicShareMapper picShareMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedisUtils redisUtils;

    static String key = "findPic";
    static String key2 = "findShare";


    //首页轮播图展示点赞数最高的五张图片
    @Override
    public BaseResp findPic() {
        //reids中是否有改数据
        BaseResp baseResp = null;
        if (!redisUtils.hasKey(key)) {
            //没有改数据，去数据库查询
            List<String> all = picShareMapper.findByTop();
            redisTemplate.opsForList().leftPushAll(key,all);
            redisTemplate.expire(key,10, TimeUnit.SECONDS);
            baseResp = new BaseResp();
            baseResp.setData(all);
            baseResp.setCode(200);
            baseResp.setMessage("查询成功");
            return baseResp;
        }else {
            //从redis里查询
            List<Object> all1 = redisUtils.lGet(key, 0, -1);
            baseResp = new BaseResp();
            /*BeanUtils.copyProperties(objects, PicShare.class);*/
            baseResp.setData(all1);
            baseResp.setCode(200);
            baseResp.setMessage("查询成功");
            return baseResp;
        }
    }

    @Override
    public BaseResp findShare() {
        List<PicShare> all = null;
        //reids中是否有改数据
        if (!redisTemplate.hasKey(key2)) {
            //没有改数据，去数据库查询
            all = picShareRespository.findAll();
            System.out.println("all = " + all);
            //存入redis
            redisTemplate.opsForList().leftPushAll(key2, all);
            //设置失效时间
            redisTemplate.expire(key2,30, TimeUnit.SECONDS);
        }
        //从redis里查询
        List<Object> objects = redisUtils.lGet(key2, 0, -1);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(objects);
        baseResp.setCode(200);
        baseResp.setMessage("查询成功");
        return baseResp;
    }


    @Override
    public BaseResp addgood(Integer id) {
        picShareMapper.addgood(id);
        //删除redis
        redisTemplate.delete(key2);
        //重新查询并存入redis
        List<PicShare> all = picShareRespository.findAll();
        redisTemplate.opsForList().leftPushAll(key2,all);
        //设置失效时间
        redisTemplate.expire(key2,30, TimeUnit.SECONDS);
        //设置返回值
        BaseResp baseResp = new BaseResp();
        baseResp.setMessage("点赞！");
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp findUserShare(Integer id) {
        Optional<PicShare> byId = picShareRespository.findById(id);
        BaseResp baseResp = new BaseResp();
        if (byId!=null){
            baseResp.setCode(200);
            baseResp.setMessage("查询成功");
            baseResp.setData(byId);
        }else{
            baseResp.setCode(100);
            baseResp.setMessage("查询失败");
        }
        return baseResp;
    }
}
